﻿Public Class Frm_Rech_Dossier
    Dim arch As Boolean = False
    Public Sub remplir()
        Partager.moi.remplir_List_info_fichier()
        Partager.list_recherch.Clear()
        List_tous.Items.Clear()
        Dim str As String = ""
        Dim cmd As New SqlClient.SqlCommand("select idRef from refAdv where idAdv=" & CInt(Partager.deAdv.Key) & _
     " and idRef in (select idRef from refClt where idClt =" & CInt(Partager.deClt.Key) & ")", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        While Partager.dr.Read
            For Each dor As Dossier In Partager.moi.List_info_fichier
                If dor.Archive = False And dor.Ref = Partager.dr("idRef") Then
                    Partager.list_recherch.Add(dor)
                    List_tous.Items.Add(New ListViewItem(New String() {dor.id, dor.Num, dor.Type_appel, dor.Tribunal}))

                    'ElseIf dor.Archive = True And dor.Ref = Partager.dr("idRef") And arch = True Then
                    'List_tous.Items.Add(New ListViewItem(New String() {dor.Num, dor.Type_appel, dor.Tribunal}))
                End If
            Next
        End While
        Partager.dr.Close()
        Partager.close()
    End Sub

    Public Sub remplirDeux()
        List_tous.Items.Clear()
        Partager.list_recherch.Clear()
        Partager.moi.remplir_List_info_fichier()
        For Each dor As Dossier In Partager.moi.List_info_fichier
            If dor.Archive = False Then
                Partager.list_recherch.Add(dor)
            End If
        Next
    End Sub
    Public Sub remplirOrig()
        List_tous.Items.Clear()
        Partager.list_recherch.Clear()
        Partager.moi.remplir_List_info_fichier()
        For Each dor As Dossier In Partager.moi.List_info_fichier
            dor.remplir_List_decisions()
            For Each dic As decisions In dor.List_decisions
                If dic.Type = "قطعي" And dor.id <> CInt(Partager.deMarji.Key) And Frm_Dossier_Orig.dv.Ref = dor.Ref Then
                  Partager.list_recherch.Add(dor)
                End If
            Next
        Next

    End Sub
   
    Private Sub Frm_Rech_Dossier_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Recherche.Enabled = True
        Frm_Idafa_Mowakilin.Enabled = True
        Frm_Dossier_Orig.Enabled = True
    End Sub

    Private Sub Frm_Rech_Dossier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        comb_matorido_elbaht.SelectedIndex = 0
        '---------------------------------------
        If Partager.type = "Frm_Recherche" And Frm_Recherche.Comb_marji3ona.Text <> "" Then
            remplir()
        ElseIf Partager.type = "Frm_Recherche" And Frm_Recherche.Comb_marji3ona.Text = "" Then
            remplirDeux()
            Partager.remplirDossier(Frm_Recherche.Comb_marji3ona)

        ElseIf Partager.type = "Frm_Idafa_Mowakilin" Then
            remplirDeux()
        ElseIf Partager.type = "Frm_Dossier_Orig" Then
            remplirOrig()
        End If

    End Sub

    Private Sub txt_rech_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_rech.TextChanged
        Try
            If Partager.type = "combo2" Then
                List_tous.Items.Clear()
                If comb_matorido_elbaht.SelectedItem = "رقم الملف" Then
                    For Each av As Dossier In Partager.list_recherch
                        av.remplir_List_decisions()
                        For Each dic As decisions In av.List_decisions
                            If dic.Type = "قطعي" Then
                                If av.Num.ToString.Contains(txt_rech.Text) Then
                                    List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                                End If
                            End If
                        Next
                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "المحكمة" Then
                    For Each av As Dossier In Partager.list_recherch
                        av.remplir_List_decisions()
                        For Each dic As decisions In av.List_decisions
                            If dic.Type = "قطعي" Then
                                If av.Tribunal.ToString.Contains(txt_rech.Text) Then
                                    List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                                End If
                            End If
                        Next

                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "نوع الدعوى" Then
                    For Each av As Dossier In Partager.list_recherch
                        av.remplir_List_decisions()
                        For Each dic As decisions In av.List_decisions
                            If dic.Type = "قطعي" Then
                                If av.Type_appel.ToString.Contains(txt_rech.Text) Then
                                    List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                                End If
                            End If
                        Next
                    Next
                End If
            Else
                List_tous.Items.Clear()
             If comb_matorido_elbaht.SelectedItem = "رقم الملف" Then
                    For Each av As Dossier In Partager.list_recherch
                        If av.Num.ToString.Contains(txt_rech.Text) Then
                            List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                        End If
                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "المحكمة" Then
                    For Each av As Dossier In Partager.list_recherch
                        If av.Tribunal.ToString.Contains(txt_rech.Text) Then
                            List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                        End If
                    Next
                ElseIf comb_matorido_elbaht.SelectedItem = "نوع الدعوى" Then
                    For Each av As Dossier In Partager.list_recherch
                        If av.Type_appel.ToString.Contains(txt_rech.Text) Then
                            List_tous.Items.Add(New ListViewItem(New String() {av.id, av.Num, av.Type_appel, av.Tribunal}))
                        End If
                    Next
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        If txt_rech.Text = "" Then
            If Partager.type = "Frm_Recherche" And Frm_Recherche.Comb_marji3ona.Text <> "" Then
                remplir()
            ElseIf Partager.type = "Frm_Recherche" And Frm_Recherche.Comb_marji3ona.Text = "" Then
                remplirDeux()
                Partager.remplirDossier(Frm_Recherche.Comb_marji3ona)

            ElseIf Partager.type = "Frm_Idafa_Mowakilin" Then
                remplirDeux()
            ElseIf Partager.type = "Frm_Dossier_Orig" Then
                remplirOrig()
            End If
        End If
    End Sub

    Private Sub Btn_choisi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_choisi.Click
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                For Each av As Dossier In Partager.list_recherch
                    If Partager.type = "Frm_Recherche" Then
                        If av.id = CInt(List_tous.SelectedItems(0).SubItems(0).Text) Then
                            Frm_Recherche.Comb_marji3ona.Text = av.Num & "  |  " & av.Type_appel & "  |  " & av.Tribunal
                            If Frm_Recherche.Comb_nom.Text = "" And Frm_Recherche.Comb_adv.Text = "" And Frm_Recherche.Comb_marji3ona.Text <> "" Then
                                Dim cmd As New SqlClient.SqlCommand("select idAdv,idClt from refClt,refAdv" & _
                                " where refClt.idRef=refAdv.idRef and refClt.idRef=" & av.Ref, Partager.con)
                                Partager.open()
                                Partager.dr = cmd.ExecuteReader
                                Dim i As Integer = 0
                                Dim j As Integer = 0
                                Dim flag As Boolean = False
                                Dim flag1 As Boolean = False
                                If Partager.dr.HasRows Then
                                    Partager.dr.Read()
                                    If Frm_Recherche.focs = "" Then
                                        For Each item In Frm_Recherche.Comb_nom.Items
                                            Partager.deClt = DirectCast(item, DictionaryEntry)
                                            If CInt(Partager.deClt.Key) = Partager.dr("idClt") Then
                                                flag = True
                                                Exit For
                                            End If
                                            i += 1
                                        Next

                                        For Each item In Frm_Recherche.Comb_adv.Items
                                            Partager.deClt = DirectCast(item, DictionaryEntry)
                                            If CInt(Partager.deClt.Key) = Partager.dr("idAdv") Then
                                                flag1 = True
                                                Exit For
                                            End If
                                            j += 1
                                        Next

                                    End If
                                End If
                                Partager.dr.Close()
                                Partager.close()
                                If flag Then
                                    Frm_Recherche.focs = "dossier"
                                    Frm_Recherche.Comb_nom.SelectedIndex = i
                                End If
                                If flag1 Then
                                    Frm_Recherche.focs = "dossier"
                                    Frm_Recherche.Comb_adv.SelectedIndex = j
                                End If
                            End If
                            Exit For
                        End If
                    ElseIf Partager.type = "Frm_Idafa_Mowakilin" Then
                        If av.id = CInt(List_tous.SelectedItems(0).SubItems(0).Text) Then
                            Frm_Idafa_Mowakilin.Comb_marji3ona.Text = av.Num & "  |  " & av.Type_appel & "  |  " & av.Tribunal
                            Exit For
                        End If
                    ElseIf Partager.type = "Frm_Dossier_Orig" Then
                        If av.id = CInt(List_tous.SelectedItems(0).SubItems(0).Text) Then
                            Frm_Dossier_Orig.Comb_marji3ona.Text = av.Num & "  |  " & av.Type_appel & "  |  " & av.Tribunal
                            Exit For
                        End If
                    End If
                Next
                Me.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ في البحث")
        End If
    End Sub

    Private Sub comb_matorido_elbaht_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comb_matorido_elbaht.SelectedIndexChanged
        txt_rech.Clear()
    End Sub
End Class